package start.spring.basic.report.service;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;

import start.spring.basic.config.CustomUidGenerator;
import start.spring.basic.dynamic.DynamicJdbc;
import start.spring.basic.dynamic.DynamicRedisDAO;
import start.spring.basic.dynamic.SpringJdbc;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.ry.core.constant.SecurityConstants;
import start.spring.basic.ry.core.context.SecurityContextHolder;
import start.spring.basic.util.PageUtil;
import start.spring.basic.util.common.BeanCopierUtil;

/**
 * 
 * ****************************************************************************
 * 业务操作层基类接口实现，提供公共的service层操作方法
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2020年4月9日
 ******************************************************************************
 */
@Service
public class BaseServiceImpl implements IBaseService {
	
	
	/**
	 * 静态JdbcTemplate操作依赖，提供公共的操作方法
	 */
	@Autowired
	protected SpringJdbc dao;
	
	/**
	 * 动态JdbcTemplate操作依赖，提供公共的操作方法
	 */
	@Autowired
	protected DynamicJdbc dynamicJdbc;
	
	/**
	 * redis操作依赖，提供公共的redis操作方法
	 */
	@Autowired
	protected DynamicRedisDAO redis;
	
	@Autowired
	protected CustomUidGenerator idGenerator;
	
	@Override
	public <T> IPage<T> getPage(PageQuery pageQuery) {
		return new Page<T>(pageQuery.getCurrent(), pageQuery.getSize());
	}

	@Override
	public <T> PageResult<T> getPageResult(IPage<T> page) {
		PageResult<T> pageResult = new PageResult<>();
		pageResult.setCurrent(page.getCurrent());
		pageResult.setPages(page.getPages());
		pageResult.setSize(page.getSize());
		pageResult.setTotal(page.getTotal());
		pageResult.setRecords(page.getRecords());
		
		return pageResult;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public <T> PageResult<T> getPageResult(IPage page, Class<T> target) {
		PageResult<T> pageResult = new PageResult<>();
		pageResult.setCurrent(page.getCurrent());
		pageResult.setPages(page.getPages());
		pageResult.setSize(page.getSize());
		pageResult.setTotal(page.getTotal());
		pageResult.setRecords(BeanCopierUtil.copyList(page.getRecords(), target));
		return pageResult;
	}
	
	@Override
	public <T> PageUtil<T> getPageUtil(PageQuery pageQuery) {
		PageUtil<T> pUtil = new PageUtil<>();
		if(pageQuery!=null) {
			pUtil.setCurrentPage(pageQuery.getCurrent());
			pUtil.setNumPerPage(pageQuery.getSize());
		}
		return pUtil;
	}
	
	@Override
	public <T> PageResult<T> getPageResult(PageUtil<T> page) {
		PageResult<T> pageResult = new PageResult<>();
		pageResult.setCurrent(page.getCurrentPage());
		pageResult.setPages(page.getTotalPages());
		pageResult.setSize(page.getNumPerPage());
		pageResult.setTotal(page.getTotalRecords());
		pageResult.setRecords(page.getResultList());
		return pageResult;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public <T> PageResult<T> getPageResult(PageUtil page, Class<T> target) {
		PageResult<T> pageResult = new PageResult<>();
		pageResult.setCurrent(page.getCurrentPage());
		pageResult.setPages(page.getTotalPages());
		pageResult.setSize(page.getNumPerPage());
		pageResult.setTotal(page.getTotalRecords());
		pageResult.setRecords(BeanCopierUtil.copyList(page.getResultList(), target));
		return pageResult;
	}

	@Override
	public DynamicRedisDAO getRedis() {
		return redis;
	}

	@Override
	public DataSource getDs(String dsKey) {
		return dynamicJdbc.getDs(dsKey);
	}
	
	@Override
    public SpringJdbc getDao() {
        return dao;
    }

	@Override
	public LoginUser getLoginUser() {
		return SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class);
	}
	
	@Override
	public SysUser getSysUser() {
		LoginUser loginUser = SecurityContextHolder.get(SecurityConstants.LOGIN_USER, LoginUser.class);
		if(loginUser!=null) {
			return loginUser.getSysUser();
		}
		return null;
	}
	
}
